
Namespace TechEdge.Controls
    Public Class TechEdge_LambdaMetre
        Implements BikiSoft.DataStream.IStreamControl, BikiSoft.DataStream.IStreamConfig, BikiSoft.DataStream.IStreamRecieve

        Private m_settings As New Settings

        Private Enum eDispStyle As Byte
            eDispLamda
            eDispAFR
        End Enum


        ''' <summary>
        ''' Specifies the control location
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ControlLocation() As WeifenLuo.WinFormsUI.Docking.DockAreas Implements BikiSoft.DataStream.IStreamControl.ControlLocation
            Get
                Return m_settings.m_ControlLocation
            End Get
            Set(ByVal value As WeifenLuo.WinFormsUI.Docking.DockAreas)
                m_settings.m_ControlLocation = value
                RaiseEvent RefreshControl(Me, Nothing)
            End Set
        End Property

        Public Property ControlOrder() As Integer Implements BikiSoft.DataStream.IStreamControl.ControlOrder
            Get

            End Get
            Set(ByVal value As Integer)

            End Set
        End Property

        Public Event RefreshControl(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamControl.RefreshControl

        Public ReadOnly Property Name1() As String Implements BikiSoft.INamed.Name
            Get
                Return "TechEdge Lambda Metre"
            End Get
        End Property

        ''' <summary>
        ''' This class defines the internal settings for this contorl which are to be saved/reloaded by BikiSoft upon
        ''' desktop settings change. The name of the control is the key to reloading the settings correctly. Use uniqeue
        ''' names for each different control type.
        ''' </summary>
        ''' <remarks></remarks>
        <Serializable()> _
        Private Class Settings
            Public m_RefreshTime As Integer = 500
            Public m_Colour As System.Drawing.Color = Drawing.Color.Red
            Public m_ControlLocation As WeifenLuo.WinFormsUI.Docking.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.DockTop
            ''' <summary>
            ''' Note: Regular Aust 92 RON Unleaded is 14.7.
            ''' U98 is 14.3! Very important!
            ''' </summary>
            ''' <remarks></remarks>
            Public m_FuelStoich As Double = 14.7
            Public m_DispStyle As eDispStyle = eDispStyle.eDispLamda
        End Class

        Public Property DataRefreshTime() As Integer Implements BikiSoft.DataStream.IStreamConfig.DataRefreshTime
            Get
                Return m_settings.m_RefreshTime
            End Get
            Set(ByVal value As Integer)
                m_settings.m_RefreshTime = value
            End Set
        End Property

        ''' <summary>
        ''' This function is called from bikisfot
        ''' </summary>
        ''' <param name="Items"></param>
        ''' <remarks></remarks>
        Public Sub InitialiseValidItems(ByVal Items() As String) Implements BikiSoft.DataStream.IStreamConfig.InitialiseValidItems

        End Sub

        ''' <summary>
        ''' This function is used when bikisoft saves or loads another desktop setting. Use this to serialize any settings classes
        ''' for persitant settings.. such as where it was last docked? screen position etc...
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property StreamControlData() As Byte() Implements BikiSoft.DataStream.IStreamConfig.StreamControlData
            Get
                Dim settings As New BikiSoft.Modules.ObjectPersistance(Of Settings)
                Dim stream As New System.IO.MemoryStream
                If settings.SaveToStream(stream, Me.m_settings) Then
                    Return stream.ToArray
                Else
                    Return Nothing
                End If

            End Get
            Set(ByVal value As Byte())
                Dim stream As New System.IO.MemoryStream(value)
                Dim settings As New BikiSoft.Modules.ObjectPersistance(Of Settings)
                settings.LoadFromStream(stream, Me.m_settings)
            End Set
        End Property

        Public ReadOnly Property StreamControlItems() As BikiSoft.KeyedStringCollectionWithEvents Implements BikiSoft.DataStream.IStreamConfig.StreamControlItems
            Get
                Return Nothing
            End Get
        End Property

        ''' <summary>
        ''' Raise this event before destorying yourself, it removes it from bikisoft's list of controls to send data to
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event Close(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Close

        ''' <summary>
        ''' This function is called if a control calls the Seek event. Use this instead of recieve stream incase you are doing
        ''' any logging to graphs etc. So obviously dont double log things..
        ''' </summary>
        ''' <param name="Stream"></param>
        ''' <remarks></remarks>
        Public Sub RecieveSeek(ByVal Stream As BikiSoft.DataStream.Stream) Implements BikiSoft.DataStream.IStreamRecieve.RecieveSeek
            Me.RecieveStream(Stream)
        End Sub

        ''' <summary>
        ''' Recieve the stream from bikisoft. This should have all the calcualted and inserted values from any IStreamReicever
        ''' that is also a producer.
        ''' </summary>
        ''' <param name="Stream"></param>
        ''' <remarks></remarks>
        Public Sub RecieveStream(ByVal Stream As BikiSoft.DataStream.Stream) Implements BikiSoft.DataStream.IStreamRecieve.RecieveStream
            If Stream.Contains("TE_Lambda") Then
                Me.uxLambda.Value = Math.Max(Math.Min((Stream("TE_Lambda").Value - 0.5) * 100, 100), 0)
                Select Case m_settings.m_DispStyle
                    Case eDispStyle.eDispAFR
                        Me.uxText.Text = String.Format("{0N2} AFR", (Stream("TE_Lambda").Value * m_settings.m_FuelStoich))
                    Case eDispStyle.eDispLamda
                        Me.uxText.Text = Stream("TE_Lambda").FormatedValue
                End Select

            Else
                Me.uxLambda.Value = 0
                Me.uxText.Text = "N/A"
            End If
        End Sub

        ''' <summary>
        ''' If you loose track of where you are, you can call this event to be updated with the current item.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event RequestRefresh(ByVal sender As Object, ByVal e As System.EventArgs) Implements BikiSoft.DataStream.IStreamRecieve.RequestRefresh

        ''' <summary>
        ''' Use this event to restirct the stream to a certain range. Used by the graphing control to restrict the zone for the "Find Nearest" lookup over
        ''' the data grid component.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event Restrict(ByVal sender As Object, ByVal e As BikiSoft.DataStream.StreamRestrictEventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Restrict

        ''' <summary>
        '''  Raise this event to cause the stream to seek to a location. Will send that stream to all other controls in bikisoft.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event Seek(ByVal sender As Object, ByVal e As BikiSoft.DataStream.StreamSeekEventArgs) Implements BikiSoft.DataStream.IStreamRecieve.Seek

        Public ReadOnly Property RecieverType() As BikiSoft.DataStream.StreamRecieveType Implements BikiSoft.DataStream.IStreamRecieve.RecieverType
            Get
                Return BikiSoft.DataStream.StreamRecieveType.eStreamConsumeOnly
            End Get
        End Property

        Public Sub New()

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            ' Stoich Point
            Me.uxUpDown.NumericUpDown.DecimalPlaces = 2
            Me.uxUpDown.NumericUpDown.Increment = 0.01
            Me.uxUpDown.NumericUpDown.Maximum = 40
            Me.uxUpDown.NumericUpDown.Minimum = 2
            Me.uxUpDown.NumericUpDown.Value = m_settings.m_FuelStoich

            AddHandler Me.uxUpDown.NumericUpDown.ValueChanged, AddressOf Me.uxUpDown_NumberChanged

            ' Display Style
            Me.uxComboSelect.SelectedIndex = m_settings.m_DispStyle

        End Sub

        ''' <summary>
        ''' Catches the number up down changes
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub uxUpDown_NumberChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            m_settings.m_FuelStoich = Me.uxUpDown.NumericUpDown.Value
        End Sub

        ''' <summary>
        ''' Changes the display style
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub uxComboSelect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxComboSelect.SelectedIndexChanged
            m_settings.m_DispStyle = Me.uxComboSelect.SelectedIndex
        End Sub

        ''' <summary>
        ''' Use this control state to specify where to place the control within BikiSoft.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ControlState() As WeifenLuo.WinFormsUI.Docking.DockState Implements BikiSoft.DataStream.IStreamControl.ControlState
            Get
                Return WeifenLuo.WinFormsUI.Docking.DockState.Unknown
            End Get
            Set(ByVal value As WeifenLuo.WinFormsUI.Docking.DockState)

            End Set
        End Property
    End Class
End Namespace
